﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using IQKS.AWP.BusinessAccessLayer;
using IQKS.AWP.BusinessObject;

public partial class Admin_ActivityMasterModify : System.Web.UI.Page
{
    Guid gudUserID = CommonUtility.GetLoggedOnUserId();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                GetActivityTypeData();
                if (Request.QueryString.Count > 0) //add
                {
                    if (Convert.ToString(Request.QueryString["agid"]) == "0")
                    {
                        lblTitle.Text = "Add Activity Master";
                        ClearData();
                    }
                    else //edit
                    {

                        lblTitle.Text = "Update Activity Master";
                        GetData(Convert.ToInt64(Request.QueryString["agid"]));
                    }
                }
            }
            catch (Exception)
            {
                Response.Redirect("~/Admin/LanguageMaster.aspx");
            }
        }
    }
    private void GetActivityTypeData()
    {
        ActivityTypeIdList.DataSource = GetActivityTypeDataSet();
        ActivityTypeIdList.DataTextField = "ActivityTypeName";
        ActivityTypeIdList.DataValueField = "ActivityTypeID";
        ActivityTypeIdList.DataBind();
        ActivityTypeIdList.Items.Insert(0, new ListItem("-- Select Activity Type --", ""));
    }

    private DataSet GetActivityTypeDataSet()
    {

        DataSet objDs;
        try
        {
            objDs = new ActivityTypeMasterBAL().GetActivityTypeMaster();
        }
        catch (Exception exception)
        {
            throw new ApplicationException(exception.Message);
        }
        return objDs;

    }   

    private void ClearData()
    {
        txtActivityName.Text = string.Empty;
        txtActivityCode.Text = string.Empty;
        chkIsactive.Checked = false;
        lblMessage.Text = string.Empty;
    }

    private void GetData(Int64 intActivityID)
    {
        ActivityMasterBAL objAMBal = new ActivityMasterBAL();
        ActivityMasterBO objAMBO = new ActivityMasterBO();
        objAMBO = objAMBal.GetActivityMaster(intActivityID);

        if (objAMBO != null)
        {
            ActivityTypeIdList.SelectedValue = Convert.ToString(objAMBO.ActivityTypeID);
            txtActivityName.Text = objAMBO.ActivityName;
            txtActivityCode.Text = objAMBO.ActivityCode;
            
            chkIsactive.Checked = Convert.ToBoolean(objAMBO.IsActive);
        }
    }
    protected void btnSave_OnClick(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            //lblMessage.Text = "";
            if (Request.QueryString.Count > 0)
            {
                ActivityMasterBAL objAMBal;
                ActivityMasterBO objAMBO;
                //Guid gudUserID = new Guid(Convert.ToString(Session["UserID"]));
                int intAgid = Convert.ToInt16(Request.QueryString["agid"]);
                string strmod = Convert.ToString(Request.QueryString["mod"]);
                string strReturnValue;

                if (strmod == "in")//add
                {
                    
                    objAMBal = new ActivityMasterBAL();
                    objAMBO = new ActivityMasterBO();


                    objAMBO.SetMethod = "INSERT";
                    objAMBO.ActivityID = 0;
                    objAMBO.ActivityTypeID = Convert.ToInt32(ActivityTypeIdList.SelectedValue.Trim());
                    objAMBO.ActivityName = txtActivityName.Text.Trim();
                    objAMBO.ActivityCode = txtActivityCode.Text.Trim();

                    objAMBO.CreatedBy = gudUserID;
                    objAMBO.ModifyBy = gudUserID;
                    objAMBO.ModifyDate = System.DateTime.Today;
                    objAMBO.IsActive = Convert.ToBoolean(chkIsactive.Checked);
                    strReturnValue = (string)objAMBal.SetActivityMaster(objAMBO);
                    if (strReturnValue == "SUCCESS")
                    {

                        ClearData();
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Record has been save succesfully.');", true);
                        //lblMessage.Text = "Record has been save succesfully.";
                        //lblMessage.ForeColor = System.Drawing.Color.Green;

                    }
                    else if (strReturnValue == "EXIST")
                    {
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Language already exists.');", true);
                        //lblMessage.Text = "Language already exists."; //EnrollResources.GetMessage(Message.InsertMessage); //     
                        //lblMessage.ForeColor = System.Drawing.Color.Red;
                    }
                    else
                    {
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Error while saving record.');", true);
                        //lblMessage.Text = "Error while saving record."; //EnrollResources.GetMessage(Message.FailedMessage); // 
                        //lblMessage.ForeColor = System.Drawing.Color.Red;
                    }


                }
                else if (strmod == "up")
                {

                    objAMBal = new ActivityMasterBAL();
                    
                    objAMBO = new ActivityMasterBO();

                    objAMBO.SetMethod = "UPDATE";
                    objAMBO.ActivityID = intAgid;
                    objAMBO.ActivityTypeID = Convert.ToInt32(ActivityTypeIdList.SelectedValue.Trim());
                    objAMBO.ActivityName = txtActivityName.Text.Trim();
                    objAMBO.ActivityCode = txtActivityCode.Text.Trim();

                    objAMBO.ModifyBy = gudUserID;
                    objAMBO.ModifyDate = System.DateTime.Today;
                    objAMBO.IsActive = Convert.ToBoolean(chkIsactive.Checked);
                    strReturnValue = (string)objAMBal.SetActivityMaster(objAMBO);
                    if (strReturnValue == "SUCCESS")
                    {

                        //ClearData();
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Record has been updated succesfully.');", true);
                        //lblMessage.Text = "Record has been updated succesfully.";
                        //lblMessage.ForeColor = System.Drawing.Color.Green;


                    }
                    else if (strReturnValue == "EXIST")
                    {
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Language already exists.');", true);
                        //lblMessage.Text = "Language already exists."; //EnrollResources.GetMessage(Message.InsertMessage); //       
                        //lblMessage.ForeColor = System.Drawing.Color.Red;
            
                    }
                    else
                    {
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Error while saving record.');", true);
                        //lblMessage.Text = "Error while saving record."; //EnrollResources.GetMessage(Message.FailedMessage); // 
                        //lblMessage.ForeColor = System.Drawing.Color.Red;

                    }
                }

            }
        }
    }
    protected void btnCancel_OnClick(object sender, EventArgs e)
    {
        if (Request.QueryString.Count > 0) //add
        {
            if (Convert.ToString(Request.QueryString["agid"]) == "0")
            {
                ClearData();
                Response.Redirect("~/Admin/ActivityMaster.aspx");
            }
            else
            {
                lblMessage.Text = string.Empty;
                GetData(Convert.ToInt64(Request.QueryString["agid"]));
                Response.Redirect("~/Admin/ActivityMaster.aspx");
            }

        }
    }
    protected void txtActivityCode_TextChanged(object sender, EventArgs e)
    {

    }
    protected void ActivityTypeIdList_SelectedIndexChanged(object sender, EventArgs e)
    {
        
    }
}